***************Data construction for the first part*****************************
/*Candidate data, election data, and 2010 census data has been downloaded and
merged at the level of the municipalities. For the merging of the candidate
data the identifiers of the TSE can be used (see candidate data). For merging
with the census data municipality codes can be used. Aggregate now the data to
the level of ethnic groups and calculate relative election and population values.*/

clear
cd ".../Brazil_candidates_replication/"
use Brazil_raw_municipality.dta, clear


*Recodings:Calculate vote shares and control for list votes and turnout

tab Office_CED, gen(Office_)
drop if Office_2!=1

collapse (first) Abbr_State_CED_CEN VotosLegenda_PED /*
*/Branca_CEN Preta_CEN Parda_CEN /*
*/Without_Specification_CEN Total_Population_CEN /*
*/(sum) Total_Votes_CED , by(Ethnicity_CD Code_Municipality_CED_CEN_PED)

save Brazil_agg_mun_ethn.dta, replace

*generate total (candidate) votes per municipality
collapse (sum) Total_Votes_CED (first) VotosLegenda_PED, by(Code_Municipality_CED_CEN_PED)
rename Total_Votes_CED tot_cand_votes
gen tot_cand_leg_votes=tot_cand_votes+VotosLegenda_PED

save Brazil_agg_mun.dta, replace

*Merge with ethnicity data from 2010 census (use 1:m because for each ethnic group)
merge 1:m Code_Municipality_CED_CEN_PED using "Brazil_agg_mun_ethn.dta"

*generate agg vote shares of ethnic groups per municipality
gen votes_branca=Total_Votes_CED/tot_cand_votes if Ethnicity_CD=="BRANCA"
gen votes_parda=Total_Votes_CED/tot_cand_votes if Ethnicity_CD=="PARDA"
gen votes_preta=Total_Votes_CED/tot_cand_votes if Ethnicity_CD=="PRETA"

*generate relative vote shares of ethnic group per municipality
gen Branca_rel_CEN=Branca_CEN/Total_Population_CEN
gen Preta_rel_CEN=Preta_CEN/Total_Population_CEN
gen Parda_rel_CEN=Parda_CEN/Total_Population_CEN

gen Legenda_rel=VotosLegenda_PED/tot_cand_leg_votes

gen turnout=tot_cand_leg_votes/Total_Population_CEN

encode Abbr_State_CED_CEN, gen(state)

*keep relevant variables and rename
keep votes_branca votes_parda votes_preta Branca_rel_CEN Preta_rel_CEN Parda_rel_CEN Legenda_rel turnout state
rename votes_branca votes_white
rename votes_parda votes_brown
rename votes_preta votes_black
rename Branca_rel_CEN pop_white
rename Preta_rel_CEN pop_black
rename Parda_rel_CEN pop_brown
rename Legenda_rel votes_legenda

*Label relevant variables
label variable votes_white "Votes Whites"
label variable votes_brown "Votes Browns"
label variable votes_black "Votes Blacks"
label variable pop_white "White population share"
label variable pop_brown "Brown population share"
label variable pop_black "Black population share"
label variable votes_legenda "Share party votes"
label variable turnout "Turnout"

save Brazil_municipality.dta, replace
